#!/usr/bin/perl
#vol_recovery.cgi

require './diskman-lib.pl';

&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);

$err_msg="";
$log_ev="";

my $volname = $in{'volname'};
$volname =~ /volume(\d+)/;
$vntmp = "RaidMirror$1";
if (!$volname) {
	#volname can't be nulls	
	$err_msg = $text{"err_cgiparam"};
	goto ERROR_OUT;
}

my $mkfs_lock;
$mkfs_lock = check_lock();
if ($mkfs_lock) {
	$err_msg = $text{"err_mkfslocked"};
	goto ERROR_OUT;
}

my (@rdinfo, @dinfo, @free_disks, $r, $rr);

@rdinfo = new_get_check_raid_info();
&foreign_require("diskman","diskman-lib.pl");
@mdinfo = &foreign_call("diskman", "get_check_raid_info");
@free_volumes = &foreign_call("diskman", "get_free_volumes", \@mdinfo);

my $found = 0;
foreach $r (@mdinfo) {
	if ($r->{'volname'} eq $volname) {
		$rr = $r;
		$found = 1;
		last;
	}
}

if (!$found) {
	#finish latter
	$err_msg = $text{"err_configchanged"};
	goto ERROR_OUT;
}

#if (!can_recovery($rr, \@dinfo)) {
#	$err_msg = $text{"err_configchanged"};
#	goto ERROR_OUT;
#}

#@free_disks = &get_free_disks(\@dinfo);
#if (!@free_disks) {
#	$err_msg = $text{"err_configchanged"};
#	goto ERROR_OUT;
#}

&header($text{'vol_recovery_title'}, "", "vol_recovery", 0, 1);
print "<form name=recoveryform action=save_mirror_recovery.cgi method=post onsubmit='return onsubmit_form();'>\n";

print "<table class=group width=100%>\n";
print "<tr> <td class=group> <table class=mode1 border=0 cellpadding=3 cellspacing=0>\n";

#volume name
print "<tr> <td width=250> <b> $text{'mirrorname'} </b> </td>\n";
print "<td width=200> $vntmp </td>\n";
print "</tr> \n";

#free disk selection
print "<tr> <td width=250> <b> $text{'free_diskarray'} </b> </td>\n";
print "<td width=200> <select name=adddisk class=drop> \n";
my ($fd, @pvinfo, $pvv, $sdpv, $pvfound);
my @pvinfo = get_pv_info();

foreach $fd (@rdinfo) {
        $mdinfo = `mdadm -Q $fd->{'sdev'} |grep raid1`;
        if($mdinfo =~ /\S+\s+2\s+device\s+\S+\s+raid1\s+/) {
                next;
        }
        $pvfound = 0;
        foreach $pvv (@pvinfo){
                #print $pvv->{'pv'};
                if($pvv->{'pv'} eq $fd->{'sdev'}){
                        $pvfound = 1;
                        $sdpv = $pvv;
                        last;
                }
        }
        if($pvfound){
                if($sdpv->{'vg'}){
                        next;
                }
        }

	if ($i == 0) {
		print "<option selected value=$fd->{'sdev'}> $fd->{'volname'} \n";
	}
	else {
		print "<option value=$fd->{'sdev'}> $fd->{'volname'}\n";
	}
	$i++;
}
print "</select> </td> </tr> \n";

print "</table> </td> </tr> \n";
#recovery button
print "<tr> <td class=group> <table class=mode1 border=0 cellpadding=3 cellspacing=0> \n";
print "<tr> <td> <input type=submit name=recoveryvolume value=\"$text{'btn_mirror_recovery'}\" class=button> </td> </tr>\n";
print "</table> </td> </tr> \n";

print "</table>\n";

print "<input type=hidden name=volname value=$volname>\n";
print "</form>\n";

print <<EOF;
<script>
function onsubmit_form()
{
        document.forms[0].recoveryvolume.disabled = true;
}

</script>
EOF

&footer("/", $text{'index'});
return;
	
ERROR_OUT:
&error_setup($text{'rec_err_title'});
&error($err_msg);

